package com.ibeifeng.crm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibeifeng.crm.bean.User;
import com.ibeifeng.crm.service.UserService;
@Controller
@RequestMapping("/pages/user/")
public class UserController {
	Logger logger=Logger.getLogger(UserController.class);
	@Resource
	UserService userService;
	@RequestMapping(value="queryAll", produces = "application/json; charset=utf-8")
	public @ResponseBody String mquery(HttpServletRequest request, HttpServletResponse response){
		String pg = request.getParameter("page");
		String pgSize = request.getParameter("rows");
		String keyWord=request.getParameter("keyWord");
		System.out.println("*************Controller查询用户*************");
		// 若要显示的第几页为空，那么默认为1
		if (null == pg || "".equals(pg)) {
			pg = "1";
		}
		// 显示多少行为空，那么默认为10
		if (null == pgSize || "".equals(pgSize)) {
			pgSize = "5";
		}		
		List<User> list = userService.queryLimit(Integer.parseInt(pg), Integer.parseInt(pgSize),keyWord);
		int count=userService.count();
		Map m=new HashMap();
		if(count>0){
			m.put("total", count);
		}else{
			m.put("total", 1);
		}
		
		m.put("rows", list);
		JSONArray js=JSONArray.fromObject(m);
		String j=js.toString();
		String jz=j.substring(1, j.length()-1);
		return jz;
	}

	@RequestMapping("addUser")
	@RequiresPermissions("1")
	public @ResponseBody String addUser( User user,String [] p){
		System.out.println("*************Controller添加用户*************");
		String str="";
		for (int i = 0; i < p.length; i++) {
			if(str==null||"".equals(str)){
				str=str+p[i];
			}else{
			str=str+","+p[i];
			}
		}
		int i=user.getUserId()+1;
		user.setUserId(i);
		user.setUserPermission(str);
		boolean f=userService.add(user);
		Map m=new HashMap();
		if(f){
			m.put("result","true");
		}else{
			m.put("msg","save error");
		}
		JSONObject js=JSONObject.fromObject(m);
		return js.toString();
	}
	@RequestMapping("delUser")
	public @ResponseBody String delUser(HttpServletRequest request, HttpServletResponse response) {
		String[] array = request.getParameterValues("array[]"); 
		List<Integer> list = new ArrayList<Integer>();
		System.out.println("*************Controller删除用户*************");
		for (int i = 0; i < array.length; i++) {  
			int id = Integer.parseInt(array[i]);
		list.add(id);  
		}	
		boolean f=userService.del(list);
		Map m=new HashMap();
		if(f){
			m.put("result","true");
		}else{
			m.put("msg","del Error");
		}
		JSONArray js=JSONArray.fromObject(m);
		String j=js.toString();
		String jz=j.substring(1, j.length()-1);
		System.out.println(jz);
		return jz;
	}
	@RequestMapping("updUser")
	public @ResponseBody String updUser(User user,String [] p){
		System.out.println("*************Controller修改用户*************");
		String str="";
		for (int i = 0; i < p.length; i++) {
			if(str==null||"".equals(str)){
				str=str+p[i];
			}else{
			str=str+","+p[i];
			}
		}
		user.setUserPermission(str);

		boolean f=userService.upd(user);
		Map m=new HashMap();
		if(f){
			m.put("result","true");
		}else{
			m.put("result","false");
		}
		JSONObject js=JSONObject.fromObject(m);
		System.out.println(js.toString());
		return js.toString();
	}
		
	
}
